void init_SPI2(void)
{
	mux_PINS(1,2,0x1);
	mux_PINS(1,3,0x1);
	mux_PINS(1,4,0x1);
	mux_PINS(2,7,0x2);
//	mux_PINS(2,10,0x2);   //363所用
	adi_spi_Open(2,&SPI2_MemorySize,ADI_SPI_MEMORY_SIZE,&handle_SPI2);
//	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	
//	uint8_t pTransmitter;
//	uint8_t pReceiver;
//	Xfr_SPI2.pTransmitter=&pTransmitter;
//	Xfr_SPI2.pReceiver=&pReceiver;
//	Xfr_SPI2.TransmitterBytes=1;
//	Xfr_SPI2.ReceiverBytes=1;
//	Xfr_SPI2.nTxIncrement=0;
//	Xfr_SPI2.nRxIncrement=0;
//	Xfr_SPI2.bDMA=false;
//	Xfr_SPI2.bRD_CTL=true;
//	adi_spi_MasterReadWrite(handle_SPI2,&Xfr_SPI2);  //阻塞
	
	adi_spi_SetMasterMode(handle_SPI2,true);
//	adi_spi_SlaveReadWrite(handle_SPI2,&Xfr_SPI2);
//	adi_spi_MasterSubmitBuffer(handle_SPI2,&Xfr_SPI2);  //非阻塞
//	adi_spi_SlaveSubmitBuffer(handle_SPI2,&Xfr_SPI2);
//	adi_spi_RegisterCallback(handle_SPI2,callback_SPI2,NULL);
	adi_spi_SetClockPhase(handle_SPI2,false);
	adi_spi_SetClockPolarity(handle_SPI2,false);
//	bool bComplete;
//	adi_spi_isBufferAvailable(handle_SPI2,&bComplete);
	adi_spi_SetContinuousMode(handle_SPI2,true);
//	adi_spi_SetLoopback(handle_SPI2,true);
//	adi_spi_SetIrqmode(handle_SPI2,0);
	adi_spi_SetReceiveOverflow(handle_SPI2,true);
	adi_spi_SetTransmitUnderflow(handle_SPI2,true);
	adi_spi_SetBitrate(handle_SPI2,1000000);
	adi_spi_SetChipSelect(handle_SPI2,ADI_SPI_CS3);   //363所用CS2
}

void init_SPI1(void)
{
	mux_PINS(1,9,0x1);
	mux_PINS(1,8,0x1);
	mux_PINS(1,7,0x1);
	mux_PINS(1,6,0x1);
	adi_spi_Open(1,&SPI1_MemorySize,ADI_SPI_MEMORY_SIZE,&handle_SPI1);
//	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	
//	uint8_t pTransmitter;
//	uint8_t pReceiver;
//	Xfr_SPI2.pTransmitter=&pTransmitter;
//	Xfr_SPI2.pReceiver=&pReceiver;
//	Xfr_SPI2.TransmitterBytes=1;
//	Xfr_SPI2.ReceiverBytes=1;
//	Xfr_SPI2.nTxIncrement=0;
//	Xfr_SPI2.nRxIncrement=0;
//	Xfr_SPI2.bDMA=false;
//	Xfr_SPI2.bRD_CTL=true;
//	adi_spi_MasterReadWrite(handle_SPI2,&Xfr_SPI2);  //阻塞
	
	adi_spi_SetMasterMode(handle_SPI1,true);
//	adi_spi_SlaveReadWrite(handle_SPI2,&Xfr_SPI2);
//	adi_spi_MasterSubmitBuffer(handle_SPI2,&Xfr_SPI2);  //非阻塞
//	adi_spi_SlaveSubmitBuffer(handle_SPI2,&Xfr_SPI2);
//	adi_spi_RegisterCallback(handle_SPI2,callback_SPI2,NULL);
	adi_spi_SetClockPhase(handle_SPI1,false);
	adi_spi_SetClockPolarity(handle_SPI1,false);
//	bool bComplete;
//	adi_spi_isBufferAvailable(handle_SPI1,&bComplete);
	adi_spi_SetContinuousMode(handle_SPI1,true);
//	adi_spi_SetLoopback(handle_SPI2,true);
//	adi_spi_SetIrqmode(handle_SPI2,0);
	adi_spi_SetReceiveOverflow(handle_SPI1,true);
	adi_spi_SetTransmitUnderflow(handle_SPI1,true);
	adi_spi_SetBitrate(handle_SPI1,1000000);
	adi_spi_SetChipSelect(handle_SPI1,ADI_SPI_CS0);   //357所用CS0
}

void init_SPI0(void)
{
	mux_PINS(0,0,0x1);
	mux_PINS(0,1,0x1);
	mux_PINS(0,2,0x1);
	mux_PINS(0,3,0x1);
	adi_spi_Open(0,&SPI0_MemorySize,ADI_SPI_MEMORY_SIZE,&handle_SPI0);
//	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	
//	uint8_t pTransmitter;
//	uint8_t pReceiver;
//	Xfr_SPI2.pTransmitter=&pTransmitter;
//	Xfr_SPI2.pReceiver=&pReceiver;
//	Xfr_SPI2.TransmitterBytes=1;
//	Xfr_SPI2.ReceiverBytes=1;
//	Xfr_SPI2.nTxIncrement=0;
//	Xfr_SPI2.nRxIncrement=0;
//	Xfr_SPI2.bDMA=false;
//	Xfr_SPI2.bRD_CTL=true;
//	adi_spi_MasterReadWrite(handle_SPI2,&Xfr_SPI2);  //阻塞
	
	adi_spi_SetMasterMode(handle_SPI0,true);
//	adi_spi_SlaveReadWrite(handle_SPI2,&Xfr_SPI2);
//	adi_spi_MasterSubmitBuffer(handle_SPI2,&Xfr_SPI2);  //非阻塞
//	adi_spi_SlaveSubmitBuffer(handle_SPI2,&Xfr_SPI2);
//	adi_spi_RegisterCallback(handle_SPI2,callback_SPI2,NULL);
	adi_spi_SetClockPhase(handle_SPI0,true);
	adi_spi_SetClockPolarity(handle_SPI0,true);
//	bool bComplete;
//	adi_spi_isBufferAvailable(handle_SPI0,&bComplete);
	adi_spi_SetContinuousMode(handle_SPI0,true);
//	adi_spi_SetLoopback(handle_SPI2,true);
//	adi_spi_SetIrqmode(handle_SPI2,0);
	adi_spi_SetReceiveOverflow(handle_SPI0,true);
	adi_spi_SetTransmitUnderflow(handle_SPI0,true);
	adi_spi_SetBitrate(handle_SPI0,1000000);
	adi_spi_SetChipSelect(handle_SPI0,ADI_SPI_CS0);   //363所用CS2
}


uint8_t SPI2_Send_x(uint8_t x,uint32_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	uint8_t i;
	uint8_t pTransmitter[4];
	uint8_t pReceiver[4];	
	
	for(i=0;i<x;i++)
	{
		pTransmitter[i]=0xFF&(word>>((x-i-1)*8));
	}
	
	Xfr_SPI2.pTransmitter=pTransmitter;
	Xfr_SPI2.pReceiver=pReceiver;
	Xfr_SPI2.TransmitterBytes=x;
	Xfr_SPI2.ReceiverBytes=x;
	Xfr_SPI2.nTxIncrement=1;
	Xfr_SPI2.nRxIncrement=1;
	Xfr_SPI2.bDMA=false;
	Xfr_SPI2.bRD_CTL=false;
	adi_spi_MasterSubmitBuffer(handle_SPI2,&Xfr_SPI2);
	
	uint32_t pHWErrors_SPI2;
	adi_spi_GetBuffer(handle_SPI2,&pHWErrors_SPI2);
	int j;
	while(pHWErrors_SPI2 != ADI_SPI_SUCCESS)
	{
		adi_spi_GetBuffer(handle_SPI2,&pHWErrors_SPI2); //查询是否完成
		if(pHWErrors_SPI2 == ADI_SPI_SUCCESS){return 1;}
		else{j++;delay_us(20);}
		if(j>5){printf("[INFO] SPI2 Error:%x\n",word);return 0;}
	}
	return 1;
}

int16_t SPI2_Send_x_Read_1(uint8_t x,uint32_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	uint8_t i;
	uint8_t pTransmitter[4];
	uint8_t pReceiver[4];	
	
	for(i=0;i<x;i++)
	{
		pTransmitter[i]=0xFF&(word>>((x-i-1)*8));
	}
	
	Xfr_SPI2.pTransmitter=pTransmitter;
	Xfr_SPI2.pReceiver=pReceiver;
	Xfr_SPI2.TransmitterBytes=x+1;
	Xfr_SPI2.ReceiverBytes=x+1;
	Xfr_SPI2.nTxIncrement=1;
	Xfr_SPI2.nRxIncrement=1;
	Xfr_SPI2.bDMA=false;
	Xfr_SPI2.bRD_CTL=false;
	adi_spi_MasterSubmitBuffer(handle_SPI2,&Xfr_SPI2);
	
	uint32_t pHWErrors_SPI2;
	adi_spi_GetBuffer(handle_SPI2,&pHWErrors_SPI2);
	int j;
	while(pHWErrors_SPI2 != ADI_SPI_SUCCESS)
	{
		adi_spi_GetBuffer(handle_SPI2,&pHWErrors_SPI2); //查询是否完成
		if(pHWErrors_SPI2 == ADI_SPI_SUCCESS){return pReceiver[x];}
		else{j++;delay_us(20);}
		if(j>5){printf("[INFO] SPI2 Error:%x\n",word);return -1;}
	}
	return pReceiver[x];
}

int32_t SPI2_Send_x_Read_2(uint8_t x,uint32_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	uint16_t Receiver_Word = 0;
	uint8_t i;
	uint8_t pTransmitter[4];
	uint8_t pReceiver[4];	
	
	for(i=0;i<x;i++)
	{
		pTransmitter[i]=0xFF&(word>>((x-i-1)*8));
	}
	
	Xfr_SPI2.pTransmitter=pTransmitter;
	Xfr_SPI2.pReceiver=pReceiver;
	Xfr_SPI2.TransmitterBytes=x+2;
	Xfr_SPI2.ReceiverBytes=x+2;
	Xfr_SPI2.nTxIncrement=1;
	Xfr_SPI2.nRxIncrement=1;
	Xfr_SPI2.bDMA=false;
	Xfr_SPI2.bRD_CTL=false;
	adi_spi_MasterSubmitBuffer(handle_SPI2,&Xfr_SPI2);

	uint32_t pHWErrors_SPI2;
	adi_spi_GetBuffer(handle_SPI2,&pHWErrors_SPI2);
	
	Receiver_Word = (0xFFFF&(pReceiver[x] << 8)) + pReceiver[x+1];
	int j;
	while(pHWErrors_SPI2 != ADI_SPI_SUCCESS)
	{
		adi_spi_GetBuffer(handle_SPI2,&pHWErrors_SPI2); //查询是否完成
		if(pHWErrors_SPI2 == ADI_SPI_SUCCESS){return Receiver_Word;}
		else{j++;delay_us(20);}
		if(j>5){printf("[INFO] SPI2 Error:%x\n",word);return -1;}
	}
	return Receiver_Word;
}

uint8_t SPI1_Send_x_Read_1(uint8_t x,uint32_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI1;
	uint8_t i;
	uint8_t pTransmitter[4];
	uint8_t pReceiver[4];	
	
	for(i=0;i<x;i++)
	{
		pTransmitter[i]=0xFF&(word>>((x-i-1)*8));
	}
	
	Xfr_SPI1.pTransmitter=pTransmitter;
	Xfr_SPI1.pReceiver=pReceiver;
	Xfr_SPI1.TransmitterBytes=x+1;
	Xfr_SPI1.ReceiverBytes=x+1;
	Xfr_SPI1.nTxIncrement=1;
	Xfr_SPI1.nRxIncrement=1;
	Xfr_SPI1.bDMA=false;
	Xfr_SPI1.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI1,&Xfr_SPI1);
	
	return pReceiver[x];
}

uint8_t SPI1_Send_x(uint8_t x,uint32_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI1;
	uint8_t i;
	uint8_t pTransmitter[4];
	uint8_t pReceiver[4];	
	
	for(i=0;i<x;i++)
	{
		pTransmitter[i]=0xFF&(word>>((x-i-1)*8));
	}
	
	Xfr_SPI1.pTransmitter=pTransmitter;
	Xfr_SPI1.pReceiver=pReceiver;
	Xfr_SPI1.TransmitterBytes=x;
	Xfr_SPI1.ReceiverBytes=x;
	Xfr_SPI1.nTxIncrement=1;
	Xfr_SPI1.nRxIncrement=1;
	Xfr_SPI1.bDMA=false;
	Xfr_SPI1.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI1,&Xfr_SPI1);
	return 1;
}
void SPI2_Send_Buf_x(uint16_t x,uint8_t buf[])
{

	ADI_SPI_TRANSCEIVER Xfr_SPI2;
	uint8_t pReceiver[x];	
	
	Xfr_SPI2.pTransmitter=buf;
	Xfr_SPI2.pReceiver=pReceiver;
	Xfr_SPI2.TransmitterBytes=x;
	Xfr_SPI2.ReceiverBytes=x;
	Xfr_SPI2.nTxIncrement=1;
	Xfr_SPI2.nRxIncrement=1;
	Xfr_SPI2.bDMA=false;
	Xfr_SPI2.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI2,&Xfr_SPI2);
}
void SPI1_Send_Buf_x(uint16_t x,uint8_t buf[])
{

	ADI_SPI_TRANSCEIVER Xfr_SPI1;
	uint8_t pReceiver[x];	
	
	Xfr_SPI1.pTransmitter=buf;
	Xfr_SPI1.pReceiver=pReceiver;
	Xfr_SPI1.TransmitterBytes=x;
	Xfr_SPI1.ReceiverBytes=x;
	Xfr_SPI1.nTxIncrement=1;
	Xfr_SPI1.nRxIncrement=1;
	Xfr_SPI1.bDMA=false;
	Xfr_SPI1.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI1,&Xfr_SPI1);
}

void SPI0_Send_2(uint16_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI0;

	uint8_t pTransmitter[2];
	uint8_t pReceiver[2];	
	
	pTransmitter[0]=0xFF&(word>>8);
	pTransmitter[1]=(0x00FF&(word))&0xFF;
	
	Xfr_SPI0.pTransmitter=pTransmitter;
	Xfr_SPI0.pReceiver=pReceiver;
	Xfr_SPI0.TransmitterBytes=2;
	Xfr_SPI0.ReceiverBytes=2;
	Xfr_SPI0.nTxIncrement=1;
	Xfr_SPI0.nRxIncrement=1;
	Xfr_SPI0.bDMA=false;
	Xfr_SPI0.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI0,&Xfr_SPI0);
}

uint16_t SPI0_Send_Read_2(uint16_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI0;

	uint8_t pTransmitter[2];
	uint8_t pReceiver[2];	
	uint16_t dat=0;
	
	pTransmitter[0]=0xFF&(word>>8);
	pTransmitter[1]=(0x00FF&(word))&0xFF;
	
	Xfr_SPI0.pTransmitter=pTransmitter;
	Xfr_SPI0.pReceiver=pReceiver;
	Xfr_SPI0.TransmitterBytes=2;
	Xfr_SPI0.ReceiverBytes=2;
	Xfr_SPI0.nTxIncrement=1;
	Xfr_SPI0.nRxIncrement=1;
	Xfr_SPI0.bDMA=false;
	Xfr_SPI0.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI0,&Xfr_SPI0);
	
	dat = ((pReceiver[0]<<8)&0xFF00)+pReceiver[1];
	return dat;
}

uint16_t SPI0_Send_2_Read_2(uint16_t word)
{
	ADI_SPI_TRANSCEIVER Xfr_SPI0;

	uint8_t pTransmitter[4];
	uint8_t pReceiver[4];	
	uint16_t dat=0;
	
	pTransmitter[0]=0xFF&(word>>8);
	pTransmitter[1]=(word&0x00FF)&0xFF;
	pTransmitter[2]=0x00;
	pTransmitter[3]=0x00;
	
	Xfr_SPI0.pTransmitter=pTransmitter;
	Xfr_SPI0.pReceiver=pReceiver;
	Xfr_SPI0.TransmitterBytes=4;
	Xfr_SPI0.ReceiverBytes=4;
	Xfr_SPI0.nTxIncrement=1;
	Xfr_SPI0.nRxIncrement=1;
	Xfr_SPI0.bDMA=false;
	Xfr_SPI0.bRD_CTL=false;
	adi_spi_MasterReadWrite(handle_SPI0,&Xfr_SPI0);
	
	dat = ((pReceiver[2]<<8)&0xFF00)+pReceiver[3];
	return dat;
}



